package monster.com.cvh.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import monster.com.cvh.R;
import monster.com.cvh.bean.FeedBackPicBean;
import monster.com.cvh.fragment.PicLookFragment;
import monster.com.cvh.util.StatusBarUtil;
import monster.com.cvh.view.PhotoViewPager;

/**
 * @author: monster
 * @data: 2018/4/4
 * @function: 图片预览
 */
public class PicLookActivity extends AppCompatActivity {
    @BindView(R.id.tv_pic_look_title)
    TextView tvPicLookTitle;
    private PhotoViewPager mVpActivityPicLook;
    private ImageView mIvPicLookBack;
    private LinearLayout mLlPicLookAll;
    private RelativeLayout mRlPicLookBar;
    private Boolean showBar = true;
    private List<Fragment> mList;
    private FeedBackPicBean feedBackPicBean;
    private Fragment fg3;
    private Fragment fg2;
    private String urlPath3;
    private String urlPath2;
    private ArrayList<Parcelable> feedBackPicBeans;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_look);
        ButterKnife.bind(this);
        initViews();
        initDatas();
        initEvent();
    }

    private void initEvent() {
        mIvPicLookBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initDatas() {
        //修改状态栏颜色
        StatusBarUtil.setStatusBarLightMode(PicLookActivity.this, Color.WHITE);
        mList = new ArrayList<>();
        //接收上个页面传过来的数据
        feedBackPicBeans = getIntent().getParcelableArrayListExtra("feedBackPicBeans");
        int index = getIntent().getIntExtra("index", 0);
        tvPicLookTitle.setText((index+1)+"/"+feedBackPicBeans.size());
        //获取第一个
        for (int i = 0; i < feedBackPicBeans.size(); i++) {
            feedBackPicBean = (FeedBackPicBean) feedBackPicBeans.get(i);
            String urlPath1 = feedBackPicBean.getUrlPath();
            Log.i("lixiaofei", "initDatas: " + urlPath1);
            Bundle bundle1 = new Bundle();
            bundle1.putInt("pager_num", 1);
            bundle1.putString("urlPath1", urlPath1);
            Fragment fg1 = PicLookFragment.newInstance(bundle1);
            mList.add(fg1);
        }

        mVpActivityPicLook.setAdapter(new MyFragmentStateAdapter(getSupportFragmentManager(), mList));
        mVpActivityPicLook.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onPageSelected(int position) {
                    tvPicLookTitle.setText(position+1+"/"+feedBackPicBeans.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mVpActivityPicLook.setCurrentItem(index);
    }


    private void initViews() {
        mVpActivityPicLook = findViewById(R.id.vp_activity_pic_look);
        mIvPicLookBack = findViewById(R.id.iv_pic_look_back);
        mLlPicLookAll = findViewById(R.id.ll_ativity_pic_look_all);
        mRlPicLookBar = findViewById(R.id.rl_pic_look_bar);
    }

    class MyFragmentStateAdapter extends FragmentStatePagerAdapter {

        List<Fragment> list;

        public MyFragmentStateAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        /**
         * 返回需要展示的fragment
         *
         * @param position
         * @return
         */
        @Override
        public Fragment getItem(int position) {
            return list.get(position);

        }

        /**
         * 返回需要展示的fangment数量
         *
         * @return
         */
        @Override
        public int getCount() {
            return list.size();
        }
    }
}
